/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package map.file;

import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import map.Map;
import figures.Figure;
import figures.FigureIterator;
import figures.basic.NameManager;
import file.FileGetter;
import geometry.Rectangle;


public class FileMap implements Map,FigureIterator {
	
	String file = null;
	boolean eof = false;
	FileGetter getter;
	long fpos = 0;
	public int nelems = -1;
	InputStream is = null;
	NameManager nm = new NameManager();
	
	//Iterator
	Figure next = null;
	
	public FileMap(FileGetter g, String f){
		file = f;
		getter = g;
	}
	
	//Map implementation
	public FigureIterator getFigures(Rectangle area) {
		try {
			if(is != null)
				is.close();
			InputStream s = getter.getResource(file);
			if(s==null)
				return null;
			is = s;
			byte[] buffer = new byte[4];
			is.read(buffer, 0, 4);
			nelems = FileMap.byteArrayToInt(buffer,0);
			nm.load(is);
			eof = false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;
	}

	//FigureIterator implementation
	public boolean hasNext() {
		while(next==null && !eof)
			try {
				next=FigureLoader.load(is, nm);
			} catch (IOException e1) {
				eof=true;
			}
		if(eof)
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return ! eof;
	}

	public Figure next() {
		while(next==null && !eof)
			try {
				next=FigureLoader.load(is, nm);
			} catch (IOException e) {
				eof=true;
			}
		Figure f = next;
		next =null;
		return f;
	}
	
	public static void skipString(InputStream is) throws IOException {
		byte len  = (byte) is.read();
		is.skip(len);
	}
	
	public static byte[] readStringBytes(InputStream is) throws IOException {
		int len  = is.read();
		if(len<0)
			throw new EOFException();
		byte[] bytes = new byte[len];
		is.read(bytes,0,len);
		return bytes;
	}
	
	public static int byteArrayToInt(byte[] b, int offset) {
        int value = (b[offset++] & 0x000000FF) << 24;
        value += (b[offset++] & 0x000000FF) << 16;
        value += (b[offset++] & 0x000000FF) << 8;
        value += (b[offset] & 0x000000FF);
        return value;
    }
	
	public static String readString(InputStream is) throws IOException {
		byte[] r=readStringBytes(is);
		if(r==null)
			return new String();
		else
			return new String(r);
	}

	public static void writeString(DataOutputStream os, String string) throws IOException {
		byte[] bb = string.getBytes();
		os.writeByte(bb.length);
		os.write(bb);
	}
}
